package com.amap.poi.service.impl;

import com.amap.poi.mapper.CityMapper;
import com.amap.poi.model.entity.City;
import com.amap.poi.service.ICityService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author L
 * @since 2023-02-19
 */
@Service
@Slf4j
public class CityServiceImpl extends ServiceImpl<CityMapper, City> implements ICityService {

    /**
     * 查询
     *
     * @param keyword
     * @return
     */
    @Override
    public Map<String, Object> queryByCondition(String keyword) {
        // 构建查询条件
        QueryWrapper<City> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .like(City::getProvince, keyword)
                .or()
                .like(City::getCities, keyword)
        ;
        List<City> list = list(queryWrapper);
        // 定义map
        Map<String, Object> map = new HashMap<>(16);
        // 遍历
        list.stream().parallel().forEach(e -> {
            // 分割城市
            String[] split = e.getCities().split(",");
            List<String> cities = new ArrayList<>(split.length);
            Collections.addAll(cities, split);
            // 赋值
            map.put(e.getProvince(), cities);
        });
        return map;
    }

}
